package edu.gdut.tiantian.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

import edu.gdut.tiantian.entity.Category;
import edu.gdut.tiantian.entity.Product;
import edu.gdut.tiantian.service.Impl.CategoryService;
import edu.gdut.tiantian.service.Impl.ProductService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping("/findAll.do")
	public ModelAndView findByCondition(Integer pageNum,HttpServletRequest req) {
		Map<String, String> condition = new HashMap<>();
		Enumeration<String> paramNames = req.getParameterNames();
		StringBuffer conditionStr = new StringBuffer();
		while(paramNames.hasMoreElements()) {
			String key = paramNames.nextElement();
			String value = req.getParameter(key);
			if(value != null && value.trim().length() !=0) {
				condition.put(key, value);
				conditionStr.append("&"+key+"="+value);
			}
		}
		ModelAndView mv = new ModelAndView();
		List<Product> productList = productService.findByCondition(condition,pageNum,5);
		PageInfo<Product> pageInfo = new PageInfo<Product>(productList);
		List<Category> categoryList = categoryService.findAll();
		mv.addObject("condition",conditionStr);
		mv.addObject("pageInfo",pageInfo);
		mv.addObject("categoryList",categoryList);
		mv.setViewName("background/product-manager");
		return mv; 
	}
	
	@RequestMapping("/save.do")
	public String save(Product product) {
		productService.save(product);
		return "redirect:/product/findAll.do?pageNum=0";
	}
	
	@RequestMapping("/update.do")
	public String update(Product product) {
		productService.update(product);
		return "redirect:/product/findAll.do?pageNum=0";
	}
	
	@RequestMapping("/findById")
	public ModelAndView findById(String productId) {
		ModelAndView mv = new ModelAndView();
		Product product = productService.findById(productId);
		mv.addObject("product",product);
		mv.setViewName("background/product-show");
		return mv;
	}
	
	@RequestMapping("/user-findById.do")
	public ModelAndView user_findById(String productId) {
		ModelAndView mv = new ModelAndView();
		Product product = productService.findById(productId);
		mv.addObject("product",product);
		mv.setViewName("user/product-show");
		return mv;
	}
	
	@RequestMapping("/delete.do")
	public String delete(String productId) {
		productService.delete(productId);
		return "redirect:/product/findAll.do?pageNum=0";
	}
	
	//获取首页商品信息
	@RequestMapping("/getIndexMsg.do")
	public ModelAndView getIndexMsg() {
		ModelAndView mv = new ModelAndView();
		List<Category> categoryList = categoryService.findAll();
		mv.addObject("categoryList",categoryList);
		List<Product> commendList = productService.getCommend();
		List<Product> newList = productService.getNew();
		mv.addObject("commendProduct",commendList);
		mv.addObject("newProduct",newList);
		mv.setViewName("/user/index-product");
		return mv;
	}
	@RequestMapping("/user-search.do")
	public ModelAndView userSerarch(Integer pageNum,HttpServletRequest req) {
		Map<String, String> condition = new HashMap<>();
		Enumeration<String> paramNames = req.getParameterNames();
		StringBuffer conditionStr = new StringBuffer();
		while(paramNames.hasMoreElements()) {
			String key = paramNames.nextElement();
			String value = req.getParameter(key);
			if(value != null && value.trim().length() !=0) {
				condition.put(key, value);
				conditionStr.append("&"+key+"="+value);
			}
		}
		ModelAndView mv = new ModelAndView();
		List<Product> productList = productService.findByCondition(condition,pageNum,8);
		PageInfo<Product> pageInfo = new PageInfo<Product>(productList);
		List<Category> categoryList = categoryService.findAll();
		mv.addObject("condition",conditionStr);
		mv.addObject("pageInfo",pageInfo);
		mv.addObject("categoryList",categoryList);
		mv.setViewName("user/product-search");
		return mv; 
	}
}
